package com.bytz.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.aspect.annotation.AutoLog;
import com.bytz.common.system.base.controller.BytzController;
import com.bytz.common.system.query.QueryGenerator;
import com.bytz.modules.system.entity.CodeGenerator;
import com.bytz.modules.system.service.ICodeGeneratorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

 /**
 * 编码生成器
 */
@Slf4j
@Api(tags="编码生成器")
@RestController
@RequestMapping("/generator/codeGenerator")
public class CodeGeneratorController extends BytzController<CodeGenerator, ICodeGeneratorService> {
	@Autowired
	private ICodeGeneratorService codeGeneratorService;
	
	/**
	 * 分页列表查询
	 *
	 * @param codeGenerator
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="编码生成器-分页列表查询", notes="编码生成器-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(CodeGenerator codeGenerator,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CodeGenerator> queryWrapper = QueryGenerator.initQueryWrapper(codeGenerator, req.getParameterMap());
		Page<CodeGenerator> page = new Page<CodeGenerator>(pageNo, pageSize);
		IPage<CodeGenerator> pageList = codeGeneratorService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param codeGenerator
	 * @return
	 */
	@AutoLog(value = "编码生成器-添加")
	@ApiOperation(value="编码生成器-添加", notes="编码生成器-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CodeGenerator codeGenerator) {
		codeGeneratorService.save(codeGenerator);
		return Result.ok("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param codeGenerator
	 * @return
	 */
	@AutoLog(value = "编码生成器-编辑")
	@ApiOperation(value="编码生成器-编辑", notes="编码生成器-编辑")
	@RequestMapping(value = "/edit", method = { RequestMethod.PUT, RequestMethod.POST })
	public Result<?> edit(@RequestBody CodeGenerator codeGenerator) {
		codeGeneratorService.updateById(codeGenerator);
		return Result.ok("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "编码生成器-通过id删除")
	@ApiOperation(value="编码生成器-通过id删除", notes="编码生成器-通过id删除")
	@RequestMapping(value = "/delete", method = { RequestMethod.DELETE, RequestMethod.POST })
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		codeGeneratorService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "编码生成器-批量删除")
	@ApiOperation(value="编码生成器-批量删除", notes="编码生成器-批量删除")
	@RequestMapping(value = "/deleteBatch", method = { RequestMethod.DELETE, RequestMethod.POST })
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.codeGeneratorService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="编码生成器-通过id查询", notes="编码生成器-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CodeGenerator codeGenerator = codeGeneratorService.getById(id);
		return Result.ok(codeGenerator);
	}
}
